<?php
/**
 * 活动小程序相关服务类
 * Actutils.php BY My
 * Created by sen at 2021/3/23
 * AgeClub.net @ 2021
 */

namespace app\api\controller\bzh\enroll;


use app\api\library\Utils;
use app\common\controller\Api;
use think\Db;
use think\Log;
use think\Validate;
use think\Cache;

class Enroll extends Api
{
    protected $noNeedLogin = ['personal_reg','newsList','lunbo','personal_active','bussiness_reg','allSearch','gettype','setRow','flow'];
    protected $noNeedRight = '*';



    /**
 * 获取展会预定
 */
    public function bussiness_reg()
    {
        if ($this->request->isPost()) {
            $name=$this->request->post('name');
            $phone=$this->request->post('phone');
//            $type=$this->request->post('type');
            $company=$this->request->post('company');
            $email=$this->request->post('email');

            if(empty($name)){
                $this->error('请填写名字');
            }
            if(empty($name)){
                $this->error('请填写名字');
            }
//            if(empty($type)){
//                $this->error('请填写名字');
//            }

            if(empty($company) ){
                $this->error('请填写正确的公司名字');
            }
            if(empty($email) ){
                $this->error('请填写正确的邮箱');
            }

            if(strlen($phone) != '11'){
                $this->error('请填写正确的联系方式');
            }
            $data=[
                'name'=>$name,
                'phone'=>$phone,
                'company'=>$company,
                'email'=>$email,



                ];

            $list = Db::name('bzh_bussiness_regist')
                ->insert($data);
            if($list){
                $this->success('报名成功','200');
            }else{
                $this->error('报名失败','304');
            }

        }

    }


    /**
     * 获取展会预定
     */
    public function personal_reg()
    {

        if ($this->request->isPost()) {

         $name=$this->request->post('name');
            $phone=$this->request->post('phone');

            if(empty($name)){
                $this->error('请填写名字','305');
            }

            if(empty($phone) ){
                $this->error('请填写正确的联系方式','305');
            }else{
                $list=Db::name('bzh_enroll')
                    ->where('phone',"$phone")
                    ->count();
                if($list>0){
                    $this->error('您已成功报名！','305');
                }
            }

//            if(strlen($phone) != '11'){
//                $this->error('请填写正确的联系方式','305');
//            }
            $data=['name'=>$name,'phone'=>$phone];

            $list = Db::name('bzh_enroll')
                ->insert($data);
            if($list){
                $this->success('报名成功','200');
            }else{
                $this->error('报名失败','304');
            }

        }

    }

    public function wan ($arr){
        $list = Db::name('bzh')->alias('asa','a')where('')

    }
    /**
     * 获取展会预定
     */
    public function personal_active()
    {

        if ($this->request->isGet()) {
            $list = Db::name('bzh_enrollset')
                ->where('status', 'normal')
                ->find();
            if(empty($list)){
                $this->error('获取描述失败', '', 3010);
            }
            $list = collection($list)->toArray();
            $result = array("rows" => $list);
            $this->success('获取分类信息成功', $result, 200);
        }

    }


    /**
     * 轮播图展示
     */
    public function lunbo()
    {
        if ($this->request->isGet()) {
            $list = Db::name('bzh_lunbo')
                ->where('status', 'normal')
                ->select();
$lists= [];
            if(!empty($list)){

                foreach ($list as $key => $value) {

                  $lists["$key"]['gourl'] = $value['url'].$value['machid'];
                    $lists["$key"]['id'] = $value['id'];
                    $lists["$key"]['image'] = $value['image'];
                    $lists["$key"]['url'] = $value['gourl'];
                    $lists["$key"]['isstatus'] = $value['isstatus'];

                }

                

                $list = collection($lists)->toArray();
                $result = array("rows" => $list);
                $this->success('获取成功', $result, 200);
            }else{
                $this->error('未上传轮播图','404');
            }

        }
    }

    public function allSearch()
    {

        if ($this->request->isGet()) {
            $title = $this->request->request('title', '');
            $page = $this->request->request('page', 1);

            if(!$title){
                $this->error('请输入搜索条件', '', 3010);
            }

            $total = Db::name('bzh_business')
                ->where('company', 'like', "%$title%")
                ->count();
            $limit = ($page-1) * 5;//翻页
            if ($total == '0') {
//                $this->error('暂无搜索结果', '', 3010);
                $list=[];
            } else {
                $list = Db::name('bzh_business')
                    ->where('company', 'like', "%$title%")
                    ->limit($limit, 5)
                    ->field('id,company,log,hit,forward,collage')
                    ->select();
            }

            $total_product = Db::name('bzh_product')
                ->where('name', 'like', "%$title%")
                ->count();

            if ($total_product == '0') {
//                $this->error('暂无产品搜索结果', '', 3010);
                $list_product=[];

            } else {
                $list_product = Db::name('bzh_product')
                ->where('name','like',"%$title%")
                ->limit($limit, 5)
                ->field('id,name,proimage,introduce')
                ->select();

            }

            $list = collection($list)->toArray();
            $list_product = collection($list_product)->toArray();
            $result = array("rowsBusiness" => $list,"rowsProduct"=>$list_product);
            $this->success('索搜成功', $result, 200);

        }

    }


    public function productList()
    {
        if ($this->request->isGet()) {
            $type = $this->request->request('type', 1);
            $page = $this->request->request('page', 1);
            $total = Db::name('bzh_product')
                ->where('status', 'normal')
                ->where('taxonomy', $type)
                ->count();

            $limit = ($page-1) * 5;
            $list = Db::name('bzh_product')
                ->where('taxonomy', $type)
                ->where('status', 'normal')
                ->limit($limit, 5)
                ->field('id,name,describe,proimage,introduce')
                ->select();

            if(empty($list)){
                $this->error('暂无分类该更多产品',406);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);
            $this->success('获取信息成功', $result, 200);
        }else{
            $this->error('请求方式有误',305);
        }

    }

    public function setRow()
    {//排位设置
        if ($this->request->isGet()) {
            $total = Db::name('bzh_setnumber')
                ->where(1)
                ->count();
            $data = [];
            for ($i = 1; $i < 10; $i++) {

                $list = Db::name('bzh_setnumber')
                    ->where('row', $i)
                    ->order('number', 'asc')
                    ->select();

                if (empty($list)) {
                    break;
                }
                $arr =[
                    '1'=>'A',
                    '2'=>'B',
                    '3'=>'C',
                    '4'=>'D',
                    '5'=>'E',
                    '6'=>'F',
                ] ;
                $bianhao = $arr["$i"];
                $data["$bianhao"]= $list;

            }

            $data = collection($data)->toArray();

            $result = array("total" => $total, "rows" => $data);
            $this->success('获取信息成功', $result, 200);

        }

    }



//    public function flow()
//    {
//        if ($this->request->isPost()) {
//            $type=$this->request->post('type');//这个选择表名
//            $user_id=$this->request->post('user_id');
//            $id=$this->request->post('id');
//            $flow=$this->request->post('flow');
////            $getstatues=$this->request->post('getstatues');
//            if($flow =='200'){
//                $list = Db::name('bzh_attention')
//                    ->where('user_id',$user_id)
//                    ->where('lei',$type)
//                    ->where('gx_id',$id)
//                    ->find();
////var_dump($list);die;
//                $this->success('获取信息成功', $list, 200);
//            }
//
//            if ($flow=='-1'){//传过来的参数就是取消关注
//                $status = 'hidden';
//                $data = [
//                    'user_id' => $user_id,
//                    'gx_id' => $id,
//                    'lei' => $type,
//                    'status'=> $status,
//                ];
//
//                $res = Db::name('bzh_attention')
//                    ->where('user_id',$user_id)
//                    ->where('lei',$type)
//                    ->where('gx_id',$id)
//                    ->update(['status' => "hidden"]);
//
//                $this->success('取消关注成功');
//            }
//
//            if(!empty($id) and !empty($type) and !empty($user_id) and $flow=='1'){
//
//                $list = Db::name('bzh_attention')
//                    ->where('user_id',$user_id)
//                    ->where('lei',$type)
//                    ->where('gx_id',$id)
//                    ->count();
//
//
//                if(  $list =='0'){
//                    $status = 'normal';
//                    $data = [
//                        'user_id' => $user_id,
//                        'gx_id' => $id,
//                        'lei' => $type,
//                    ];
//                    $sql=  Db::name('bzh_attention')->insert($data);
//                    if($sql) {
//                        $this->success('关注成功');
//                    }
//
//                }
//            }else{
//                $this->error('参数错误');
//            }
//
//
//        }
//
//    }
    public function flow()
    {

        if ($this->request->isPost()) {
            $type=$this->request->post('type');//这个选择表名
            $user_id=$this->request->post('user_id');
            $id=$this->request->post('id');
            $flow=$this->request->post('flow');

//            $getstatues=$this->request->post('getstatues');
            if($flow =='200'){
                $list = Db::name('bzh_attention')
                    ->where('user_id',$user_id)
                    ->where('lei',$type)
                    ->where('gx_id',$id)
                    ->find();
//var_dump($list);die;
                $this->success('获取信息成功', $list, 200);
            }

            if ($flow=='-1'){//传过来的参数就是取消关注
                $status = 'hidden';
                $data = [
                    'user_id' => $user_id,
                    'gx_id' => $id,
                    'lei' => $type,
                    'status'=> $status,
                ];

                $res = Db::name('bzh_attention')
                    ->where('user_id',$user_id)
                    ->where('lei',$type)
                    ->where('gx_id',$id)
                    ->update(['status' => "hidden"]);

                $this->success('取消关注成功');
            }

            if(!empty($id) and !empty($type) and !empty($user_id) and $flow=='1'){

                $list = Db::name('bzh_attention')
                    ->where('user_id',$user_id)
                    ->where('lei',$type)
                    ->where('gx_id',$id)
                    ->count();


                    if(  $list =='0'){
                        $status = 'normal';
                        $data = [
                            'user_id' => $user_id,
                            'gx_id' => $id,
                            'lei' => $type,
                        ];
                        $sql=  Db::name('bzh_attention')->insert($data);
                        if($sql) {
                            $this->success('关注成功');
                        }
                    }else{
                        $change = Db::name('bzh_attention')
                            ->where('user_id',$user_id)
                            ->where('lei',$type)
                            ->where('gx_id',$id)
                            ->where('status','hidden')
                            ->count();
                        if($change =='1'){

                          $ch=  Db::name('bzh_attention')
                                ->where('user_id',$user_id)
                                ->where('lei',$type)
                                ->where('gx_id',$id)
                                ->update(['status' => 'normal']);
                            if($ch){
                                $this->success('关注成功');
                            }
                        }

                    }
            }else{
                $this->error('关注成功');
            }


        }

    }

}
